<?php 
defined('G_IN_SYSTEM')or exit('No permission resources.');
System::load_app_class('base','member','no');
System::load_sys_fun('user');
System::load_app_fun('my');
System::load_app_fun('user');
@ini_set("memory_limit","-1");
class autolottery extends SystemAction {
	
	public function __construct() {	
		$this->db=System::load_sys_class('model');
	}	
		
	//限时揭晓
	public function init(){
	
		//header("location: ".WEB_PATH);
		System::load_sys_fun('user');
		$title="限时揭晓"."_".$this->_cfg["web_name"];
		$jinri_time=abs(date("m")).'月'.date("d")."日";		
		$minri_time=abs(date("m",strtotime("+1 day"))).'月'.date("d",strtotime("+1 day"))."日";
		
		$w_jinri_time = strtotime(date('Y-m-d'));
		$w_minri_time = strtotime(date('Y-m-d',strtotime("+1 day")));
		$w_hinri_time = strtotime(date('Y-m-d',strtotime("+2 day")));		
		
		$jinri_shoplist = $this->db->GetList("select * from `@#_shoplist` where `xsjx_time` > '$w_jinri_time' and `xsjx_time` < '$w_minri_time' order by id DESC limit 0,3");
		$minri_shoplist = $this->db->GetList("select * from `@#_shoplist` where `xsjx_time` > '$w_minri_time' and `xsjx_time` < '$w_hinri_time' order by id DESC limit 0,3");	
		
		//往期回顾
		$endshoplist = $this->db->GetList("select * from `@#_shoplist` where `xsjx_time` != '0' and `q_uid` != '0' order by `xsjx_time` DESC limit 0,8");

		include templates("index","autolottery");
	}
		
	//ajax 商品揭晓(详情页倒计时走完触发)  yjmp
	public function autolottery_ret_install(){	
		
		if(!isset($_POST['shopid'])){
			echo '-1';exit;
		}
			
		$id = intval($_POST['shopid']);		
		$this->db->Autocommit_start();	
		$shop_info = $this->db->GetOne("select * from `@#_shoplist` where `id` = '$id' for update");
		
		if(!$shop_info){
			echo '-1'; exit;
		}			
		if($shop_info['xsjx_time'] > time()){
			echo "-4";exit;
		}	
		
		if($shop_info['canyurenshu']=='0'){
			//$shop_info['canyurenshu'] = rand(1,$shop_info['zongrenshu']);			
			echo '-3';exit;
		}			
		if(!empty($shop_info['q_user_code']) && ($shop_info['q_showtime'] == 'Y')){				
				echo '-6';exit;
		}			
		if(!empty($shop_info['q_user_code']) && ($shop_info['q_showtime'] == 'N')){				
			echo $shop_info['q_user_code']."sdfdsfsdfdsfsdf"; exit;
		}
		
			
		$shop_info['xsjx_time']=$shop_info['xsjx_time'].'.000';	
		
		//新版中奖逻辑  yjmp
		$yj_code = System::load_app_class('yjcode','pay');

		//时时彩中奖信息
		$code_info = $yj_code->get_lottery_code();
		//测试数据
		//$code_info = array();
		// $code_info['issue'] = '170606045'; 时时彩期数
		// $code_info['numbers'] = '4,9,6,6,7'; 时时彩中奖号
		// $code_info['createdAt'] = '2017-06-06 14:47:48'; 开奖时间
		
		//中奖时间的时间戳
		$lottery_time = strtotime($code_info['createdAt']);	
		$lottery_code = str_replace(",","",$code_info['numbers']);
		$lottery_pos = $code_info['issue'];

		$time_sum = $shop_info['q_counttime'];

		//生成中奖号码
		$final_code = $time_sum+$lottery_code;
		$final_code = fmod((float)$final_code,$shop_info['money']) + 1;
		//查询中奖用户信息
		
		$record_list = $this->db->GetList(" select * from `@#_member_go_record` where shopid = '$id'");
		$u_go_info = array();
		$record_id = 0;
		foreach ($record_list as $k => $v) {
			$code_arr = explode(',', $v['goucode']);
			foreach ($code_arr as $k1 => $v1) {
				if ($v1 == $final_code) {
					$u_go_info = $this->db->GetOne(" select * from `@#_member` where uid = '$v[uid]'");
					$record_id = $v['id'];
					break;
				}
			}
		}
		//中奖的用户信息
		$u_info = $this->db->GetOne("select * from `@#_member` where `uid` = '$u_go_info[uid]'");
		$u_info['username'] = _htmtocode($u_info['username']);
		$q_user = serialize($u_info);
		$q_uid = $u_info['uid'];
			
		$q_1 = $this->db->Query("UPDATE `@#_shoplist` SET								
								`q_uid` = '$q_uid',
								`q_user` = '$q_user',
								`q_user_code` = '$final_code',
								`q_end_time` = '$shop_info[xsjx_time]',
								`lot_time` = '$lottery_code',
								`lot_pos` = '$lottery_pos',
								`q_showtime` = 'Y'
								 where `id` = '$id'");
								 
		if($record_id){
			$q_2 = $this->db->Query("UPDATE `@#_member_go_record` SET `huode` = '$final_code' where `id` = '$record_id'");
		}else{
			$q_2 = true;
		}
		
		//$q_3 = $this->autolottery_install($shop_info);
		if($q_1 && $q_2){					
				$this->db->Autocommit_commit();
				//echo $code."云购码";exit;
				echo '-6';exit;
		}else{
			$this->db->Autocommit_rollback();					
			echo '-2';exit;				
		}					
	}
	
	private function suan_zd_code($gid,$r_code){	
		$codes = $this->db->GetList("select goucode from `@#_member_go_record` where `shopid` = '$gid'");
		if(empty($codes))return false;		
		$html = '';
		foreach($codes as $cv){
			$html .= $cv['goucode'].',';
		}			
		if(empty($codes))return false;		
		$codes = explode(',',$html);	
		array_pop($codes);
		asort($codes);	//正序		
		unset($html);		
		$go_code  = $r_code;
		if($go_code > end($codes)){
				$zd_jin_code = end($codes);		
		}else{			
			$t=90000000;
			foreach($codes as $k=>$v){				
					$s = abs($go_code-$v);				
					if($s <= $t){			
						$t = $s; $zd_jin_code = $v;				
					}else{
						break;
					}
			}		
		}			
		unset($codes);
		return $zd_jin_code;
	}
	//自动增加下一期数据
	private function autolottery_install($shop=null){
		if($shop['qishu'] < $shop['maxqishu']){		
			$time = time();
			System::load_app_fun("content",G_ADMIN_DIR);		
			$goods = $shop;
			$qishu = $goods['qishu']+1;
			$shenyurenshu = $goods['zongrenshu'] - $goods['def_renshu'];
			$query_table = content_get_codes_table();
			
			/*限购与云购码序列处理 start yjmp*/

			$limit_buy = $goods['limit_buy'];
			$ygcode_list = '';

			for ($i=1; $i <=$goods['money']; $i++) { 
					$ygcode_list .= $i;
				if ($i != $goods['money']) {
					$ygcode_list .= ',';
				}
			}

			/*限购与云购码序列处理 end yjmp*/

			$q_1 = $this->db->Query("INSERT INTO `@#_shoplist` (`sid`,`cateid`, `brandid`, `title`, `title_style`, `title2`, `keywords`, `description`, `money`, `yunjiage`, `zongrenshu`, `canyurenshu`,`shenyurenshu`,`def_renshu`, `qishu`,`maxqishu`,`thumb`, `picarr`, `content`,`codes_table`,`xsjx_time`,`renqi`,`pos`, `time`,`limit_buy`,`ygcode_list`)
					VALUES
					('$goods[sid]','$goods[cateid]','$goods[brandid]','$goods[title]','$goods[title_style]','$goods[title2]','$goods[keywords]','$goods[description]','$goods[money]','$goods[yunjiage]','$goods[zongrenshu]','$goods[def_renshu]','$shenyurenshu','$goods[def_renshu]','$qishu','$goods[maxqishu]','$goods[thumb]','$goods[picarr]','$goods[content]','$query_table','0','$goods[renqi]','$goods[pos]','$time','$limit_buy','$ygcode_list')
					");				
			$id = $this->db->insert_id();		
			$q_2 = content_get_go_codes($goods['zongrenshu'],3000,$id);
			if($q_1 && $q_2){
				return true;
			}else{
				return false;
			}
		}
		return true;
	}
}
?>